package com.cwz.gateway.handler;

import com.cwz.core.exception.captcha.CaptchaException;
import com.cwz.core.result.R;
import com.cwz.gateway.service.ValidateCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.HandlerFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

import java.util.Map;

/**
 * @program: w-demo
 * @description: 验证码获取
 * @author: Wen
 **/
@Component
public class ValidateCodeHandler implements HandlerFunction<ServerResponse> {
	@Autowired
	private ValidateCodeService validateCodeService;

	@Override
	public Mono<ServerResponse> handle(ServerRequest request) {
		R<Map<String, Object>> result;
		try {
			result = validateCodeService.createCaptcha();
		} catch (CaptchaException e) {
			return Mono.error(e);
		}
		return ServerResponse.status(HttpStatus.OK).body(BodyInserters.fromValue(result));
	}
}
